﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Reflection;

namespace AGrandVenture
{
    static class PauseScreenUI
    {
        public static int InventoryItemSelected = 0;
        public static int EquippedItemSelected = 0;
        public static float selectionWhirlRotation = 0.0f;
        public static List<InventoryItem> OnScreenItems = new List<InventoryItem>();
        public static int slidingRenderWindow = 0;
        public static int digitalInputMenuTraversalSpeed = 8;
        public static int menuTraversalRateLimit = 0;
        public enum MenuTab { Items, Status, Skills, Quests, System };
        public enum SortTab { ALL, WPN, ARM, ITM, RSC, QST };
        public static MenuTab CurrentMenuTab = 0;
        public static SortTab CurrentSortTab = 0;

        public static void Initialize()
        {
            foreach (InventoryItem item in Player.Character.InventoryList)
            {
                addMenuItem(item);
            }
        }

        public static void Suspend()
        {
            OnScreenItems.Clear();
        }
        #region Menu Management
        public static void sortInventory(string sortType)
        {
            OnScreenItems.Clear();
            foreach (InventoryItem item in Player.Character.InventoryList)
            {
                switch (sortType)
                {
                    case "ALL": addMenuItem(item); break;
                    case "WPN": if (item.Type == InventoryItem.ItemType.Weapon) addMenuItem(item); break;
                    case "ARM": if (item.Type == InventoryItem.ItemType.Armor ||
                                    item.Type == InventoryItem.ItemType.Shield) addMenuItem(item); break;
                    case "ITM": if (item.Type == InventoryItem.ItemType.Consumable) addMenuItem(item); break;
                    case "RSC": if (item.Type == InventoryItem.ItemType.Resource) addMenuItem(item); break;
                    case "QST": if (item.Type == InventoryItem.ItemType.Quest) addMenuItem(item); break;
                    default: break;
                }
                
            }
        }

        public static void addMenuItem(InventoryItem newItem)
        {
            if (newItem != null)
                OnScreenItems.Add(newItem);
        }

        public static void removeMenuItem(InventoryItem item)
        {
            if (item != null)
                OnScreenItems.Remove(item);
        }
        #endregion

        #region Draw
        public static void drawPauseMenu(SpriteBatch spriteBatch)
        {
            selectionWhirlRotation -= .15f;
            selectionWhirlRotation %= 6.35f;

            spriteBatch.Begin();
            //Draw background
            spriteBatch.Draw(Game1.pauseScreen, new Rectangle(0, 0, 1280, 720), Color.White);

            drawMenuChoiceButtons(spriteBatch);
            drawEquippedItems(spriteBatch);
            if (CurrentMenuTab == MenuTab.Items)
            {
                drawSelectedItemInfo(spriteBatch);
                drawInventoryList(spriteBatch);
                drawInventorySortMenu(spriteBatch);
            }
            if (CurrentMenuTab == MenuTab.Status)
                drawCharacterStatus(spriteBatch);
            spriteBatch.End();
        }

        public static void drawSelectedItemInfo(SpriteBatch spriteBatch)
        {
            if (OnScreenItems.Count > 0)
            {
                SpriteFont fieldFont = Game1.fontMiramonte12;
                SpriteFont valueFont = Game1.fontSegoe14;
                int screenX = 455, 
                    screenY = 470;

                int screenYInc = 20,
                    screenXInc = 20;

                spriteBatch.Draw(Game1.selectedItemInfoFrame, new Rectangle(440, 454, 349, 246), Color.White);

                InventoryItem item = OnScreenItems[(InventoryItemSelected)];

                //For all items, draw Name, Type and Level
                spriteBatch.DrawString(Game1.fontFranklin14,
                        item.Name,
                        new Vector2(screenX, screenY), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                spriteBatch.DrawString(fieldFont,
                        "Type: " + item.Type.ToString(),
                        new Vector2(screenX, (screenY += screenYInc)), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                spriteBatch.DrawString(fieldFont,
                        "Item Level: " + item.Level,
                        new Vector2(screenX + 245, screenY), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                
                //Remaining info is dependent on item type
                switch (item.Type)
                {
                    case InventoryItem.ItemType.Weapon:
                        //Primary stats
                        spriteBatch.DrawString(fieldFont,
                            "Dmg Type: " + ((MeleeWeaponItem)item).DamageType,
                            new Vector2(screenX, (screenY += screenYInc)), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                        spriteBatch.DrawString(fieldFont,
                            "Range: " + ((MeleeWeaponItem)item).TotalRange,
                            new Vector2(screenX + (screenXInc * 10), screenY), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                        spriteBatch.DrawString(fieldFont,
                            "Dmg: " + ((MeleeWeaponItem)item).TotalMinDmg + " - " + ((MeleeWeaponItem)item).TotalMaxDmg,
                            new Vector2(screenX, (screenY += screenYInc)), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                        spriteBatch.DrawString(fieldFont,
                            "Speed: " + ((MeleeWeaponItem)item).TotalAttackSpeed,
                            new Vector2(screenX + (screenXInc*5), screenY), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                        spriteBatch.DrawString(fieldFont,
                            "Crit %: " + ((MeleeWeaponItem)item).TotalCritRate,
                            new Vector2(screenX + (screenXInc * 10), screenY), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);

                        drawItemBonusStats(spriteBatch, screenX, ref screenY, item, !item.IsEquipped);

                        break;
                    case InventoryItem.ItemType.Armor:
                    case InventoryItem.ItemType.Shield:
                        spriteBatch.DrawString(fieldFont,
                            "AC:",
                            new Vector2(screenX, (screenY += screenYInc)), 
                            Color.Black, 
                            0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                        spriteBatch.DrawString(fieldFont,
                            ((ArmorItem)item).TotalAc.ToString(),
                            new Vector2(screenX+fieldFont.MeasureString("AC:").X, screenY), 
                            (((ArmorItem)item).TotalAc >= 0 ? Color.Black : Color.Red), 
                            0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                        drawItemBonusStats(spriteBatch, screenX, ref screenY, item, !item.IsEquipped);
                        break;
                    case InventoryItem.ItemType.Consumable: break;
                    case InventoryItem.ItemType.Quest: break;
                    case InventoryItem.ItemType.Resource: break;
                        
                }
            }
        }

        private static void  drawItemBonusStats(SpriteBatch spriteBatch, int screenX, ref int screenY, InventoryItem item, bool bDrawComparison)
        {
            string sStat = "";
            double dNewItemValue = 0;
            double dEquipValue = 0;
            InventoryItem equippedItem = null;
            int iStatNum = 0;
            bool bFoundBonuses = false;
            bool bCompareStats = false;
            string sList = "";
            String sNewValue = "";
            String sEquippedValue = "";
            SpriteFont fieldFont = Game1.fontMiramonte12;
            SpriteFont valueFont = Game1.fontSegoe14;

            int screenYInc = 20,
                screenXInc = 20;

            //Divider
            spriteBatch.Draw(Game1.whiteDotSheet, new Rectangle(screenX, (screenY += screenYInc) - 5, 315, 3), Color.Black);
            spriteBatch.DrawString(valueFont,
                "Item Bonuses: ",
                new Vector2(screenX, screenY), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);

            screenY += screenYInc;
            //Read bonus stats that exist
            foreach (PropertyInfo propertyInfo in item.GetType().GetProperties())
            {
                if (propertyInfo.CanRead)
                {
                    sStat = propertyInfo.Name;

                    if (sStat.Contains("Bonus"))
                    {
                        object firstValue = propertyInfo.GetValue(item, null);

                        //Check to see if item is already equipped in this item's slot
                        if (bDrawComparison && Player.Character.isEquipSlotOccupied(item.EquipSlot))
                        {
                            bCompareStats = true;
                            equippedItem = Player.Character.getItemInSlot(item.EquipSlot);
                            dEquipValue = (double)propertyInfo.GetValue(equippedItem, null);
                        }

                        if ((double)firstValue != 0)
                        {
                            bFoundBonuses = true;
                            sList += sStat;//Add to list of displayed stats
                            sStat = propertyInfo.Name.Replace("Bonus", "").Replace("AttackSpeed", "AtkSpd").Replace("lAttack", "l Atk").Replace("Resistance", " Rst").Replace("Strike", "").Replace("Magical", "M").Replace("Physical", "P").Replace("Critical", "Crit").Replace("Range", "Rng");
                            sStat = sStat.Replace("Fire", "F").Replace("Cold", "C").Replace("Mental", "M").Replace("Arcane", "A").Replace("Poison", "P").Replace("Disease", "D");
                            sStat = sStat.Replace("Luck", "LCK").Replace("Strength", "STR").Replace("Agility", "AGL").Replace("Intelligence", "INT").Replace("Constitution", "CON").Replace("Perception", "PER").Replace("Charisma", "CHA");
                            dNewItemValue = (double)firstValue;
                            sNewValue = String.Format("{0:0.#}", dNewItemValue);

                            spriteBatch.DrawString(fieldFont,
                                sStat + ":",
                                new Vector2(screenX + (screenXInc * iStatNum * 5), screenY), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);

                            spriteBatch.DrawString(fieldFont,
                                sNewValue,
                                new Vector2(screenX + (screenXInc * iStatNum * 5) + (fieldFont.MeasureString(sStat + ":").X), screenY),
                                (dEquipValue > dNewItemValue ? Color.Red : dEquipValue == dNewItemValue ? Color.Black : Color.Green),
                                0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);


                            if (bCompareStats && bDrawComparison)
                            {
                                sEquippedValue = String.Format("{0:0.#}", dEquipValue);

                                spriteBatch.DrawString(fieldFont,
                                    "(",
                                    new Vector2(screenX + (screenXInc * iStatNum * 5) + (fieldFont.MeasureString(sStat + ":" + sNewValue).X), screenY),
                                    Color.Black,
                                    0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                                spriteBatch.DrawString(fieldFont,
                                    sEquippedValue,
                                    new Vector2(screenX + (screenXInc * iStatNum * 5) + (fieldFont.MeasureString(sStat + ":" + sNewValue+"(").X), screenY),
                                    (dEquipValue >= dNewItemValue ? Color.Black : Color.Red),
                                    0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                                spriteBatch.DrawString(fieldFont,
                                    ")",
                                    new Vector2(screenX + (screenXInc * iStatNum * 5) + (fieldFont.MeasureString(sStat + ":" + sNewValue + "("+sEquippedValue).X), screenY),
                                    Color.Black,
                                    0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                            }

                            if (iStatNum++ >= 2)
                            {
                                screenY += screenYInc;
                                iStatNum = 0;
                            }

                        }
                    }

                }
            }
            if (iStatNum != 0)
                screenY += screenYInc;
            iStatNum = 0;

            if (bDrawComparison && bCompareStats)//Display any bonuses equipment has that this item is lacking
            {
                foreach (PropertyInfo propertyInfo in equippedItem.GetType().GetProperties())
                {
                    if (propertyInfo.CanRead)
                    {
                        sStat = propertyInfo.Name;

                        if (sStat.Contains("Bonus"))
                        {
                            object firstValue = propertyInfo.GetValue(equippedItem, null);

                            if ((double)firstValue > 0)
                            {
                                if (!sList.Contains(sStat))
                                {
                                    bFoundBonuses = true;
                                    sStat = propertyInfo.Name.Replace("Bonus", "").Replace("AttackSpeed", "AtkSpd").Replace("lAttack", "l Atk").Replace("Resistance", " Rst").Replace("Strike", " %").Replace("Magical", "M").Replace("Physical", "P").Replace("Critical", "Crit").Replace("Range", "Rng");
                                    sStat = sStat.Replace("Fire", "F").Replace("Cold", "C").Replace("Mental", "M").Replace("Arcane", "A").Replace("Poison", "P").Replace("Disease", "D");
                                    sStat = sStat.Replace("Luck", "LCK").Replace("Strength", "STR").Replace("Agility", "AGL").Replace("Intelligence", "INT").Replace("Constitution", "CON").Replace("Perception", "PER").Replace("Charisma", "CHA");
                                    dEquipValue = (double)firstValue;
                                    dNewItemValue = (double)propertyInfo.GetValue(item, null);
                                    sNewValue = String.Format("{0:0.#}", dNewItemValue);
                                    sEquippedValue = String.Format("{0:0.#}", dEquipValue);

                                    spriteBatch.DrawString(fieldFont,
                                        sStat + ":",
                                        new Vector2(screenX + (screenXInc * iStatNum * 5), screenY), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                                    spriteBatch.DrawString(fieldFont,
                                        sNewValue,
                                        new Vector2(screenX + (screenXInc * iStatNum * 5) + (fieldFont.MeasureString(sStat + ":").X), screenY), Color.Red, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
                                    spriteBatch.DrawString(fieldFont,
                                        "(" + sEquippedValue + ")",
                                        new Vector2(screenX + (screenXInc * iStatNum * 5) + (fieldFont.MeasureString(sStat + ":" + sNewValue).X), screenY),
                                        (dEquipValue >= 0 ? Color.Black:Color.Red),
                                        0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);

                                    if (iStatNum++ >= 2)
                                    {
                                        screenY += screenYInc;
                                        iStatNum = 0;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (!bFoundBonuses)
                spriteBatch.DrawString(fieldFont,
                    "None", new Vector2(screenX + (screenXInc * iStatNum * 4), screenY), Color.Black, 0.0f, new Vector2(0, 0), 0.8f, SpriteEffects.None, 0.0f);
        }

        private static void drawCharacterStatus(SpriteBatch spriteBatch)
        {
            int screenX1 = 875;
            int screenX2 = 950;
            int screenY = 125;
            int vSpace = 32;

            SpriteFont fieldFont = Game1.fontMiramonte12;
            SpriteFont valueFont = Game1.fontSegoe14;
            Color fieldColor = Color.Black;
            Color valueColor = Color.DarkSlateBlue;
            

            //Character name
            spriteBatch.DrawString(fieldFont,
                    "Name:",
                    new Vector2(screenX1, screenY),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(valueFont,
                    Player.Character.Name,
                    new Vector2(screenX2, screenY-4),
                    valueColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);

            
            //Title, if exists
            if (Player.Character.Title.Length > 0)
            {
                spriteBatch.DrawString(valueFont,
                        ", \"" + Player.Character.Title + "\"",
                        new Vector2(screenX2 + valueFont.MeasureString(Player.Character.Name).X, screenY - 4),
                        valueColor,
                        0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            }

            //Race
            spriteBatch.DrawString(fieldFont,
                    "Race:",
                    new Vector2(screenX1, (screenY += vSpace)),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(valueFont,
                    Player.Character.Race.ToString(),
                    new Vector2(screenX2, screenY-4),
                    valueColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);

            //Gender
            spriteBatch.DrawString(fieldFont,
                    "Gender:",
                    new Vector2(screenX1+200, screenY),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(valueFont,
                    Player.Character.Gender.ToString(),
                    new Vector2(screenX2+225, screenY - 4),
                    valueColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);

            //Class
            spriteBatch.DrawString(fieldFont,
                    "Class:",
                    new Vector2(screenX1, (screenY += vSpace)),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(valueFont,
                    Player.Character.Class.ToString(),
                    new Vector2(screenX2, screenY - 4),
                    valueColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);

            //Alignment
            spriteBatch.DrawString(fieldFont,
                    "Alignment:",
                    new Vector2(screenX1 + 200, screenY),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(valueFont,
                    Player.Character.Alignment.ToString(),
                    new Vector2(screenX2 + 225, screenY - 4),
                    valueColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);

            //Hp
            spriteBatch.DrawString(fieldFont,
                    "HP:",
                    new Vector2(screenX1, (screenY += vSpace)),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(valueFont,
                    Player.Character.RemainingHP + " / " + Player.Character.TotalHP,
                    new Vector2(screenX2, screenY - 4),
                    valueColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);

            //Energy
            spriteBatch.DrawString(fieldFont,
                    "Energy:",
                    new Vector2(screenX1 + 200, screenY),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(valueFont,
                    Player.Character.RemainingEnergy + " / " + Player.Character.RemainingEnergy,
                    new Vector2(screenX2 + 225, screenY - 4),
                    valueColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);

            //Status
            spriteBatch.DrawString(fieldFont,
                    "Status:",
                    new Vector2(screenX1, (screenY += vSpace)),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(valueFont,
                    Player.Character.Status.ToString(),
                    new Vector2(screenX2, screenY - 4),
                    valueColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);

            //Divider
            spriteBatch.Draw(Game1.whiteDotSheet, new Rectangle(screenX1-50, screenY-20, 400, 3), Color.Black);

            //Stat Header
            spriteBatch.DrawString(fieldFont,
                    "Stat |",
                    new Vector2(screenX1+10, (screenY += vSpace)),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(fieldFont,
                    "(Base  +",
                    new Vector2(screenX1+65, screenY),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(fieldFont,
                    "Bonus)",
                    new Vector2(screenX1+126, screenY),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(fieldFont,
                    "= Total",
                    new Vector2(screenX1+175, screenY),
                    fieldColor,
                    0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            //Horizontal Divider
            spriteBatch.Draw(Game1.whiteDotSheet, new Rectangle(screenX1 - 50, screenY - 30, 400, 3), Color.Black);

            List<string> stats = new List<string> { "STR", "AGI", "INT", "CON", "CHA", "PER", "LCK" };
            foreach (string stat in stats)
            {
                spriteBatch.DrawString(fieldFont,
                        stat,
                        new Vector2(screenX1+10, (screenY += vSpace)),
                        fieldColor,
                        0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                spriteBatch.DrawString(fieldFont,
                        Player.Character.getBase(stat),
                        new Vector2(screenX1 + 75, screenY),
                        Color.Black,
                        0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                spriteBatch.DrawString(fieldFont,
                        " + ",
                        new Vector2(screenX1 + 107, screenY),
                        Color.Black,
                        0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                spriteBatch.DrawString(fieldFont,
                        Player.Character.getBonus(stat),
                        new Vector2(screenX1 + 145, screenY),
                        Color.Green,
                        0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                spriteBatch.DrawString(fieldFont,
                        " = ",
                        new Vector2(screenX1 + 170, screenY),
                        Color.Black,
                        0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                spriteBatch.DrawString(fieldFont,
                        Player.Character.getTotal(stat),
                        new Vector2(screenX1 + 200, screenY),
                        Color.Blue,
                        0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            }
            //Left Vertical Divider
            spriteBatch.Draw(Game1.whiteDotSheet, new Rectangle(screenX1-50, 234, 3, 250), Color.Black);

            //Middle Vertical Divider
            spriteBatch.Draw(Game1.whiteDotSheet, new Rectangle(screenX1 + 190, 234, 3, 250), Color.Black);

            //Right Vertical Divider
            spriteBatch.Draw(Game1.whiteDotSheet, new Rectangle(screenX1 + 347, 235, 3, 250), Color.Black);

            //Horizontal Divider
            spriteBatch.Draw(Game1.whiteDotSheet, new Rectangle(screenX1 - 50, screenY - 25, 400, 3), Color.Black);

            stats = new List<string> {"Armor", "Crit %", "P. Attack", "M. Attack"};
            int statCount = 0;
            screenY += vSpace;
            foreach (string stat in stats)
            {
                if (statCount++ % 2 == 1)
                {
                    spriteBatch.DrawString(fieldFont,
                            stat + ":",
                            new Vector2(screenX1+250, screenY),
                            fieldColor,
                            0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.DrawString(fieldFont,
                            Player.Character.getTotal(stat),
                            new Vector2(screenX2+250, screenY),
                            valueColor,
                            0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                    screenY += vSpace;
                }
                else
                {
                    spriteBatch.DrawString(fieldFont,
                            stat + ":",
                            new Vector2(screenX1, screenY),
                            fieldColor,
                            0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.DrawString(fieldFont,
                            Player.Character.getTotal(stat),
                            new Vector2(screenX2, screenY),
                            valueColor,
                            0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                }
            }
            //Divider
            spriteBatch.Draw(Game1.whiteDotSheet, new Rectangle(screenX1 - 50, screenY - 45, 150, 3), Color.Black);
            spriteBatch.DrawString(fieldFont,
                            "Resistances",
                            new Vector2(screenX1+160, screenY-3),
                            fieldColor,
                            0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.Draw(Game1.whiteDotSheet, new Rectangle(screenX1 + 200, screenY - 45, 150, 3), Color.Black);

 
            stats = new List<string> { "Mental", "Arcane", "Poison", "Disease", "Fire", "Cold" };
            statCount = 0;
            screenY += vSpace;
            foreach (string stat in stats)
            {
                if (statCount++ % 2 == 1)
                {
                    spriteBatch.DrawString(fieldFont,
                            stat + ":",
                            new Vector2(screenX1 + 250, screenY),
                            fieldColor,
                            0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.DrawString(fieldFont,
                            Player.Character.getTotal(stat),
                            new Vector2(screenX2 + 250, screenY),
                            valueColor,
                            0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                    screenY += vSpace;
                }
                else
                {
                    spriteBatch.DrawString(fieldFont,
                            stat + ":",
                            new Vector2(screenX1, screenY),
                            fieldColor,
                            0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                    spriteBatch.DrawString(fieldFont,
                            Player.Character.getTotal(stat),
                            new Vector2(screenX2, screenY),
                            valueColor,
                            0.0f, new Vector2(50, 50), 1.0f, SpriteEffects.None, 0.0f);
                }
            }
        }




        private static void drawMenuChoiceButtons(SpriteBatch spriteBatch)
        {
            int screenX = 800;
            int screenY = 5;

            foreach (MenuTab tab in Enum.GetValues(typeof(MenuTab)))
            {

                spriteBatch.Draw((CurrentMenuTab == tab ? Game1.selectedMenuChoiceBubble : Game1.menuChoiceBubble),
                new Rectangle(screenX + (int)tab * 90, screenY, 90, 30), Color.White);

                spriteBatch.DrawString(Game1.fontMiramonte12,
                        tab.ToString(),
                        new Vector2(screenX + 90+ (int)tab * 88, screenY+72),
                        (CurrentMenuTab == tab ? Color.Black : Color.White),
                        0.0f,
                        new Vector2(50, 50),
                        1.5f,
                        SpriteEffects.None,
                        0.0f);
            }
        }

        private static void drawInventorySortMenu(SpriteBatch spriteBatch)
        {
            int screenX = 808;
            int screenY = 35;

            foreach (SortTab tab in Enum.GetValues(typeof(SortTab)))
            {

                spriteBatch.Draw((CurrentSortTab == tab ? Game1.selectedSubmenuFrame : Game1.submenuFrame),
                new Rectangle(screenX + (int)tab * 60, screenY, 60, 20), Color.White);

                spriteBatch.DrawString(Game1.fontMiramonte12,
                        tab.ToString(),
                        new Vector2(screenX + 65 + (int)tab * 59, screenY+50),
                        (CurrentSortTab == tab ? Color.Black : Color.White),
                        0.0f,
                        new Vector2(50, 50),
                        1.0f,
                        SpriteEffects.None,
                        0.0f);
            }
        }

        private static void drawEquippedItems(SpriteBatch spriteBatch)
        {
            int itemNumber = 1, 
                x=0, 
                y=0;

            foreach (InventoryItem item in Player.Character.EquippedList)
            {
                bool firstRing = false;
                bool firstTrinket = false;

                switch (item.EquipSlot)
                {
                    case InventoryItem.EquipmentSlot.Head: x = 305; y = 104; break;
                    case InventoryItem.EquipmentSlot.Neck: x = 305; y = 160; break;
                    case InventoryItem.EquipmentSlot.Shoulders: x = 239; y = 150; break;
                    case InventoryItem.EquipmentSlot.Back: x = 373; y = 152; break;
                    case InventoryItem.EquipmentSlot.Hands: x = 65; y = 180; break;
                    case InventoryItem.EquipmentSlot.Arms: x = 138; y = 180; break;
                    case InventoryItem.EquipmentSlot.Finger: if (!firstRing) { x = 556; y = 157; firstRing = true; }
                                                             else { x = 556; y = 210; } break;
                    case InventoryItem.EquipmentSlot.Chest: x = 305; y = 230; break;
                    case InventoryItem.EquipmentSlot.Waist: x = 305; y = 310; break;
                    case InventoryItem.EquipmentSlot.Primary: x = 148; y = 318; break;
                    case InventoryItem.EquipmentSlot.Offhand: x = 455; y = 318; break;
                    case InventoryItem.EquipmentSlot.Trinket: if (!firstTrinket) { x = 217; y = 383; firstTrinket = true; }
                                                              else { x = 400; y = 378; } break;
                    case InventoryItem.EquipmentSlot.Legs: x = 308; y = 438; break;
                    case InventoryItem.EquipmentSlot.Feet: x = 305; y = 593; break;
                }

                //Draw equipped item frame
                spriteBatch.Draw(Game1.equippedItemFrame,
                                new Rectangle(x-4, y-4, 55, 55),
                                Color.White);

                //Draw selected item whirl
                if (itemNumber == EquippedItemSelected)
                        spriteBatch.Draw(Game1.selectedItemWhirl,
                                        new Rectangle(x + 28, y + 29, 50, 50),
                                        null,
                                        Color.White,
                                        selectionWhirlRotation,
                                        new Vector2(25, 25),
                                        SpriteEffects.None,
                                        0);  

                //Draw item to screen for equipment
                spriteBatch.Draw(
                    item.Base.Texture,
                    new Vector2(x, y),
                    item.Base.Source,
                    item.Base.TintColor,
                    0.0f,
                    Vector2.Zero,
                    (itemNumber == EquippedItemSelected ? .75f: 1f),
                    SpriteEffects.None,
                    0);


                itemNumber++;
            }
        }

        private static void drawInventoryList(SpriteBatch spriteBatch)
        {
            //Reference area for drawing inventory
            int x = (int)(Game1._SCREEN_WIDTH * 0.637);
            int y = (int)(Game1._SCREEN_HEIGHT * 0.1);

            //Track which item is being reference and total number
            int itemNumber = 0;
            int totalItems = OnScreenItems.Count;

            //Slide render window to show the 10 items in range of player's selection
            if ((InventoryItemSelected >= 10) &&
                (!(slidingRenderWindow > InventoryItemSelected - 10)))
                slidingRenderWindow = InventoryItemSelected - 9;
            if (slidingRenderWindow > InventoryItemSelected)
                slidingRenderWindow = InventoryItemSelected;

            foreach (InventoryItem item in OnScreenItems)
            {
                if ((itemNumber >= slidingRenderWindow) &&
                   (y <= Game1._SCREEN_HEIGHT - 50))
                {
                    //Draw inventory item frame
                    spriteBatch.Draw((itemNumber++ == InventoryItemSelected ?
                                        Game1.selectedInventoryItemFrame :
                                        Game1.inventoryItemFrame),
                                    new Rectangle(x, y, 420, 55),
                                    Color.White);                                    

                    //Draw item to screen for inventory
                    spriteBatch.Draw(
                        item.Base.Texture,
                        new Vector2(x + 5, y + 5),
                        item.Base.Source,
                        item.Base.TintColor);

                    //Draw item name to screen
                    spriteBatch.DrawString(Game1.fontPericles8,
                                            item.Name,
                                            new Vector2(x + 120, y + 50),
                                            Color.Black,
                                            0.0f,
                                            new Vector2(50, 50),
                                            1.0f,
                                            SpriteEffects.None,
                                            0.0f);
                    //Draw item level to screen
                    spriteBatch.DrawString(Game1.fontPericles8,
                                            "Item Level: "+item.Level.ToString(),
                                            new Vector2(x + 385, y + 50),
                                            Color.Black,
                                            0.0f,
                                            new Vector2(50, 50),
                                            1.0f,
                                            SpriteEffects.None,
                                            0.0f);

                    //Draw item type to screen
                    spriteBatch.DrawString(Game1.fontPericles8,
                                            "Item Type: " +item.Type.ToString(),
                                            new Vector2(x + 120, y + 70),
                                            Color.Black,
                                            0.0f,
                                            new Vector2(50, 50),
                                            1.0f,
                                            SpriteEffects.None,
                                            0.0f);

                    //Draw item value to screen
                    spriteBatch.DrawString(Game1.fontPericles8,
                                            "Innate Value: "+item.BaseValue.ToString(),
                                            new Vector2(x + 120, y + 90),
                                            Color.Black,
                                            0.0f,
                                            new Vector2(50, 50),
                                            1.0f,
                                            SpriteEffects.None,
                                            0.0f);
                    //Draw item weight to screen

                    spriteBatch.DrawString(Game1.fontPericles8,
                                            "Weight: "+item.Weight.ToString()+" stone",
                                            new Vector2(x + 365, y + 90),
                                            Color.Black,
                                            0.0f,
                                            new Vector2(50, 50),
                                            1.0f,
                                            SpriteEffects.None,
                                            0.0f);
                    y += 62;
                }
                else
                    itemNumber++;
            }
        }
        #endregion
        #region InputHandling
        public static void handlePauseInput()
        {
            if (menuTraversalRateLimit == 0)
            {
                //Item traversal
                if (InventoryItemSelected > 0 &&
                                            (InputHandler.KeyDown(Keys.Up) ||
                                             InputHandler.ButtonPressed(Buttons.DPadUp, PlayerIndex.One)))
                {//Up
                    menuTraversalRateLimit = digitalInputMenuTraversalSpeed;
                    InventoryItemSelected--;
                }
                else if (InventoryItemSelected < OnScreenItems.Count - 1 &&
                                            (InputHandler.KeyDown(Keys.Down) ||
                                            InputHandler.ButtonPressed(Buttons.DPadDown, PlayerIndex.One)))
                {//Down
                    menuTraversalRateLimit = digitalInputMenuTraversalSpeed;
                    InventoryItemSelected++;
                }

                //Menu traversal
                if (CurrentMenuTab > 0 &&
                                    (InputHandler.KeyDown(Keys.Q) ||
                                     InputHandler.ButtonPressed(Buttons.LeftTrigger, PlayerIndex.One)))
                {//Left
                    CurrentMenuTab--;
                    menuTraversalRateLimit = digitalInputMenuTraversalSpeed;
                }
                else if ((int)CurrentMenuTab < Enum.GetValues(typeof(MenuTab)).Length - 1 &&
                                  (InputHandler.KeyDown(Keys.E) ||
                                   InputHandler.ButtonPressed(Buttons.RightTrigger, PlayerIndex.One)))
                {//Right
                    CurrentMenuTab++;
                    menuTraversalRateLimit = digitalInputMenuTraversalSpeed;
                }

                //SubMenu traversal
                if (CurrentSortTab > 0 &&
                                    (InputHandler.KeyDown(Keys.A) ||
                                     InputHandler.KeyDown(Keys.Left) ||
                                     InputHandler.ButtonPressed(Buttons.LeftShoulder, PlayerIndex.One)))
                {//Left
                    CurrentSortTab--;
                    sortInventory(CurrentSortTab.ToString());
                    InventoryItemSelected = 0;
                    menuTraversalRateLimit = digitalInputMenuTraversalSpeed;
                }
                else if ((int)CurrentSortTab < Enum.GetValues(typeof(SortTab)).Length - 1 &&
                                  (InputHandler.KeyDown(Keys.D) ||
                                   InputHandler.KeyDown(Keys.Right) ||
                                   InputHandler.ButtonPressed(Buttons.RightShoulder, PlayerIndex.One)))
                {//Right
                    CurrentSortTab++;
                    sortInventory(CurrentSortTab.ToString());
                    InventoryItemSelected = 0;
                    menuTraversalRateLimit = digitalInputMenuTraversalSpeed;
                }

            }
            else
                menuTraversalRateLimit--;
        }
        #endregion
    }
}
